/* Assignment Notebook
 * Created By: Amanda Rush
 * www.AmandaRush.com
 * Version 1.1
 * Release: 4/14/2011
 */

package com.captainkat.asn.view;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import java.util.Calendar;
import java.util.TimeZone;

import android.widget.CheckBox;
import android.widget.TextView;
import java.text.SimpleDateFormat;

import com.captainkat.asn.bo.EventHolder;
import com.captainkat.asn.calendar.CalendarManager;
import com.captainkat.asn.R;

import android.graphics.Typeface;

public class AssignmentView extends Activity implements OnClickListener {
	//CALENDAR THINGS
	private TextView date;
	private  TextView startTime;
	private  TextView endTime;
	private  TextView title;
	private  CheckBox complete;
	private  TextView desc;
	private  TextView edit;
	private  TextView back;
	private  Calendar currentDate;
	private  Calendar assignmentDate;
	private  Calendar assignmentEndDate;
	private  TimeZone timezone;
	private  SimpleDateFormat dateFormat;
	private  SimpleDateFormat timeFormat;
	private int eid;
	private Typeface font;
	
	private CalendarManager calendarManager;
	
	
	private  String calendarId = "-1";
	
	private  EventHolder eventHolder;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setRequestedOrientation(1);  
		setContentView(R.layout.assignment);
		
		font = Typeface.createFromAsset(getAssets(), "dandelion_in_the_spring.ttf");  

		//EventHolder
		eventHolder = new EventHolder();
		
		//Set up the date
		currentDate = Calendar.getInstance();
		assignmentDate= Calendar.getInstance();
		assignmentEndDate =Calendar.getInstance();
		
		//GET OUT OF BUNDLE
		Bundle b = this.getIntent().getExtras();
		calendarId = b.getString("calendarId");
		currentDate.setTimeInMillis(b.getLong("time"));
		eid = b.getInt("eid");
		
		
		timezone = TimeZone.getDefault();
		currentDate.setTimeZone(timezone);
		assignmentDate.setTimeZone(timezone);
		dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		timeFormat = new SimpleDateFormat("hh:mm a");
	
		//set up views and click listeners
		date = (TextView) findViewById(R.id.asnDate);
		date.setOnClickListener(this);
		date.setTypeface(font);
		
		startTime =(TextView) findViewById(R.id.asntime);
		startTime.setOnClickListener(this);
		startTime.setTypeface(font);

		
		endTime = (TextView) findViewById(R.id.asnendtime);
		endTime.setOnClickListener(this);
		endTime.setTypeface(font);

		
		title = (TextView) findViewById(R.id.asnTitle);
		title.setOnClickListener(this);
		title.setTypeface(font);

		
		complete = (CheckBox) findViewById(R.id.chkComplete);
		complete.setOnClickListener(this);
		complete.setTypeface(font);

		
		desc = (TextView) findViewById(R.id.description);
		desc.setOnClickListener(this);
		desc.setTypeface(font);

		
		edit = (TextView) findViewById(R.id.btnEdit);
		edit.setOnClickListener(this);
		edit.setTypeface(font);

		
		back = (TextView) findViewById(R.id.btnBack);
		back.setOnClickListener(this);
		back.setTypeface(font);

		
		calendarManager = new CalendarManager();
		calendarManager.init(this);
		
		drawScreen();
	}
	
	public void drawScreen() {
		//clear the Assignments
		title.setText("");
		desc.setText("");
		
		//put the date on the screen
		date.setText(dateFormat.format(currentDate.getTime()));
		
		calendarManager.createEventsListforDate(currentDate);
		
		eventHolder = calendarManager.getEventByIdFromManagedList(Integer.toString(eid));
	
		assignmentDate.setTimeInMillis(eventHolder.getDtstart());
		assignmentEndDate.setTimeInMillis(eventHolder.getDtend());
		
		//show dates
		date.setText(dateFormat.format(assignmentDate.getTime()));
		
		if (eventHolder.getAllDayEvent() == 1) {
			startTime.setText("All");
			endTime.setText("Day");
		} else {
			startTime.setText(timeFormat.format(assignmentDate.getTime()));
			endTime.setText(timeFormat.format(assignmentEndDate.getTime()));
		}
		title.setText(eventHolder.getTitle());	
		desc.setText(eventHolder.getDescription());
		
		//check complete status, disable checker
		/*if (title.getText().length() >0)
			if (title.getText().charAt(0) == '\u2713')
				complete.setEnabled(false);*/
		if (title.getText().length() >0){
			if (title.getText().charAt(0) == '\u2713')
				complete.setChecked(true);
		} else complete.setChecked(false);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Bundle b;
		switch(v.getId()) {
		
	
		case R.id.chkComplete:
			if (complete.isChecked()) {
				if (title.getText().length() >0) {
					if (title.getText().charAt(0) != '\u2713'){ //if it is not checked, check it
						eventHolder.setTitle('\u2713'+ title.getText().toString());
					} 
				} else {
					eventHolder.setTitle('\u2713'+" ");
				}
			} else {
				if (title.getText().length() >0) {
					if (title.getText().charAt(0) == '\u2713'){ //if it is checked, uncheck it
						eventHolder.setTitle(title.getText().toString().substring(1));
					} 
				} 
			}
			
			calendarManager.updateEvent(eventHolder);
			drawScreen();
			
			break;
		
		case R.id.btnEdit:
			Intent edit = new Intent(v.getContext(), AssignmentEdit.class);
			//add date and stuff once you have this function
			b = new Bundle();
			b.putLong("time",currentDate.getTimeInMillis());
			b.putInt("eid",Integer.parseInt(eventHolder.getEid()));
			b.putString("calendarId", calendarId);
			b.putBoolean("editing", true); 
			edit.putExtras(b);
            startActivity(edit);
            finish();
			break;
		case R.id.btnBack:
			Intent back = new Intent(v.getContext(), DailyView.class);
			b = new Bundle();
			b.putLong("time",currentDate.getTimeInMillis());
			back.putExtras(b);
            startActivity(back);
            finish();
			break;
			
		}
		
		
		}

	}

